import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_card.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_title_content_view.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/modules/application/education/quiz/quiz_controller.dart';
import 'package:enterprise_v6_app/app/modules/application/education/quiz/result/model.dart';
import 'package:enterprise_v6_app/app/modules/application/education/quiz/widget.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';

const _bgBlue = Color(0xFF5C6DFF);

// ignore: must_be_immutable
class EducationQuizResultView extends StatelessWidget {
  EducationQuizResultView({super.key}) {
    /// 因为可能从其他页面进入
    try {
      controller = Get.find<EducationQuizController>();
    } catch (_) {}
    final args = Get.arguments ?? {};
    data = controller.config.result ?? (args['data'] as QuizResultPageData?);
  }

  late final EducationQuizController controller;

  /// 结果页所需数据
  QuizResultPageData? data;

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: controller,
      builder: (_) {
        return Scaffold(
          appBar: CustomAppBar(
            backgroundColor: _bgBlue,
            iconButtonColor: Colors.white,
            leading: IconButton(
              icon: const Icon(
                Icons.arrow_back,
                color: Colors.white,
              ),
              onPressed: () {
                Get.back();
                Get.back();
              },
            ),
            title: Text(
              '查看',
              style: TextStyle(color: Colors.white, fontSize: 16.w),
            ),
          ),
          body: Stack(
            children: [
              Column(
                children: [
                  Container(
                    color: _bgBlue,
                    height: 174.w,
                  ),
                  Expanded(
                    child: Container(
                      color: AppColors.Pure_gray_bg,
                    ),
                  ),
                ],
              ),
              _buildBody(),
              Positioned(bottom: 0, left: 0, right: 0, child: _buildBottom()),
            ],
          ),
        );
      },
    );
  }

  Widget _buildBody() {
    return ListView(
      padding: EdgeInsets.all(17.w),
      children: [
        _buildCard(),
        AppGaps.vGap50,
      ],
    );
  }

  /// 主要部分 卡片
  Widget _buildCard() {
    return CustomTitleContentView(
      title: _buildCardTop(),
      content: _buildCardBottom(),
    );
  }

  /// 卡片的上部分
  Widget _buildCardTop() {
    final children = <Widget>[];
    final keys = data?.summary?.keys.toList() ?? [];
    for (int idx = 0; idx < keys.length; idx++) {
      children.add(
        _buildCardTopCount(
          keys[idx],
          data?.summary?[keys[idx]] ?? 0,
          idx == 0
              ? Alignment.centerLeft
              : idx == keys.length - 1
                  ? Alignment.centerRight
                  : Alignment.center,
        ),
      );
    }
    return Column(
      children: [
        Container(
          padding: EdgeInsets.symmetric(horizontal: 13.w, vertical: 7.w),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: children,
          ),
        ),
        AppGaps.vGap10,
        CustomCard.builtinDivider,
        AppGaps.vGap10,
        Container(
          padding: EdgeInsets.all(13.w),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      Image.asset(
                        data?.image ?? AppImages.icon_red_cal,
                        height: 13.w,
                        width: 13.w,
                      ),
                      AppGaps.hGap6,
                      Text(
                        data?.title ?? '本次错题',
                        style: TextStyle(fontSize: 16.w),
                      ),
                    ],
                  ),
                  Text(
                    data?.subtitle ?? '趁热打铁，及时巩固本次错题',
                    style: TextStyle(fontSize: 12.w, color: const Color(0xFF495061)),
                  )
                ],
              ),
              if (data?.onTapTitle != null)
                Image.asset(
                  AppImages.icon_rect_blue_right_arrow,
                  height: 17.w,
                  width: 17.w,
                ),
            ],
          ),
        ).clickOnce(() {
          data?.onTapTitle?.call();
          controller.update();
        })
      ],
    );
  }

  /// 卡片 上部分 的题目计数
  Widget _buildCardTopCount(String type, dynamic count, Alignment align) {
    return Container(
      width: 47.w,
      alignment: align,
      child: Column(
        children: [
          Text(
            count.toString(),
            style: TextStyle(
              //fontFamily: 'YouSheBiaoTiHei-Bold',
              fontSize: 16.w,
              color: _bgBlue,
            ),
          ),
          Text(
            type,
            style: TextStyle(fontSize: 11.w, color: const Color(0xFF495061)),
          )
        ],
      ),
    );
  }

  /// 卡片的下部分
  Widget _buildCardBottom() {
    return Container(
      padding: EdgeInsets.all(13.w),
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                '答题卡',
                style: TextStyle(fontSize: 18.w, fontWeight: FontWeight.w500),
              ),
              QuizWidgets.statusIndicator,
            ],
          ),
          AppGaps.vGap10,
          _buildCardBottomQuizSummary(),
        ],
      ),
    );
  }

  Widget _buildCardBottomQuizSummary() {
    return SizedBox(
      height: 56.w * ((controller.config.topics.length / 5).ceil() + 1),
      width: 343.w,
      child: GridView(
        physics: const NeverScrollableScrollPhysics(),
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 5),
        children: [
          for (int idx = 0; idx < controller.config.topics.length; idx++) QuizWidgets.topicDotIndicator(controller.config.topics[idx], idx),
        ],
      ),
    );
  }

  Widget _buildBottom() {
    if (controller.config.textbookId == null) return const SizedBox();
    return Container(
      width: 375.w,
      decoration: BoxDecoration(
        color: Colors.white,
        boxShadow: [
          BoxShadow(
            color: const Color(0x0D000000),
            offset: Offset(0, -3.w),
            blurRadius: 2.w,
            spreadRadius: 0,
          ),
        ],
      ),
      child: Padding(
        padding: EdgeInsets.symmetric(vertical: 5.w, horizontal: 16.w),
        child: InkWell(
          onTap: () async {
            Get.toNamed(AppRoutes.quizHistoryPage, arguments: controller.config.textbookId);
          },
          child: Container(
            padding: EdgeInsets.symmetric(vertical: 9.w),
            decoration: BoxDecoration(
              color: _bgBlue,
              borderRadius: BorderRadius.circular(44.w),
            ),
            alignment: Alignment.center,
            child: Text(
              '历史记录',
              style: TextStyle(
                fontSize: 16.w,
                color: Colors.white,
              ),
            ),
          ),
        ),
      ),
    );
  }
}
